/**
 * Created by Brant on 2016/12/28.
 */
module.exports = function ($http,httpservice,$q) {
    var caseBarDetailjinchengService = {};




    /*******************拖拽排序****************************************/
    caseBarDetailjinchengService.getsortsortable = function (startArr) {
        var params = startArr;

        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateTaskWorkBenchUrl,
            data: params,
            headers: {'Content-Type': 'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    /***********************************************************/


    //获取列表
    caseBarDetailjinchengService.getList = function (caseId) {
        var params={"caseId":caseId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getAJJCListUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //添加进程
    caseBarDetailjinchengService.addTask = function (params) {//title,caseId,caseStageId,deadLine,executorId,creatorId,userName
        // var params={"title":title,"caseId":caseId,"caseStageId":caseStageId,"deadLine":deadLine,"executorId":null,"creatorId":creatorId,"userName":userName};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addJCTaskUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //完成进程
    caseBarDetailjinchengService.updateTask = function (taskId,status,title,creatorId,caseId,userName) {
        var params={"taskId":taskId,"status":status,"title":title,"creatorId":creatorId,"caseId":caseId,"userName":userName};
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateTaskUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //删除进程
    caseBarDetailjinchengService.deleteTask = function (taskId) {
        var params={"taskId":taskId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteTaskUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //获取详情列表
    caseBarDetailjinchengService.getDetailList = function (taskId) {
        var params={"taskId":taskId};
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getJCDetailUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //详情上传描述或者日期
    caseBarDetailjinchengService.updateDateOrDes = function (params) {//taskId,content,deadLine,title,text,creatorId,caseId,userName,taskEstimateHour,taskActualHour
        // var params={
        //   "taskId":taskId,
        //   "content":content,
        //   "deadLine":deadLine,
        //   "title":title,
        //   'text':text,
        //   "creatorId":creatorId,
        //   "caseId":caseId,
        //   "userName":userName,
        //   "estimateHour":taskEstimateHour,
        //   'actualHour':taskActualHour
        // };
        var deferred = $q.defer();
        $http({
            method: 'PUT',
            url: httpservice.updateDetailTaskUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //获取律师列表
    caseBarDetailjinchengService.getLawyers = function (caseId,detailTaskId) {
        var params={"caseid":caseId,"taskId":detailTaskId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.getLawyersUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    // 添加律师执行人
    caseBarDetailjinchengService.addTaskLawyer = function (taskId,executorId,executorName,userId,userName,caseId) {
        var params={"taskId":taskId,"executiveId":executorId,"executorName":executorName,"userId":userId,"userName":userName,"caseId":caseId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.addTaskExecutorUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    // 删除律师执行人
    caseBarDetailjinchengService.deleteTaskLawyer = function (lawyerId,userId,userName,executiveid,executiveName,taskId) {
        var params={"id":lawyerId,"userId":userId,"userName":userName,"executiveId":executiveid,"executorName":executiveName,"taskId":taskId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteTaskExecutorUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //发说说
    caseBarDetailjinchengService.sendJCComment = function (commentUserId,comment,taskId) {
        var params={"commentUserId":commentUserId,"comment":comment,"taskId":taskId};
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.sendJCCommentUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    // 获取任务日志
    caseBarDetailjinchengService.getTaskLog = function (taskId) {
        var params={"taskId":taskId };
        var deferred = $q.defer();
        $http({
            method: 'GET',
            url: httpservice.getTaskLogListUrl,
            params:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    // 上传附件
    caseBarDetailjinchengService.uploadAppendix = function (fileData) {
        var deferred = $q.defer();
        $http({
            method: 'POST',
            url: httpservice.uploadTaskFileUrl,
            data:fileData,
            headers: {'Content-Type': undefined},
            transformRequest: angular.identity
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    // 下载附件
    caseBarDetailjinchengService.downloadAppendix = function (params) {
        // var params={"fileName":fileName};
        // var deferred = $q.defer();
        // $http({
        //     method: 'POST',
        //     url: httpservice.downLoadTaskFileUrl,
        //     params:params,
        //     responseType: 'arraybuffer',
        //     headers : {'Content-Type':'application/json'}
        // }).then(function successCallback(response) {
        //     deferred.resolve(response.data);
        // }, function errorCallback(response) {
        //     deferred.reject(response);
        // });
        // return deferred.promise;
      return $http({
        method:"POST",
        url:httpservice.downLoadTaskFileUrl,
        params:params,
        responseType: 'arraybuffer',
        headers:{'Content-Type':'application/json'},
      })
    };

    // 删除附件
    caseBarDetailjinchengService.deleteTaskAppendix = function (fileId,fileName,taskId,creatorId) {
        var params={"taskAttsId":fileId,"fileName":fileName,"taskId":taskId,"creatorId":creatorId};
        var deferred = $q.defer();
        $http({
            method: 'DELETE',
            url: httpservice.deleteTaskFileUrl,
            data:params,
            headers : {'Content-Type':'application/json'}
        }).then(function successCallback(response) {
            deferred.resolve(response.data);
        }, function errorCallback(response) {
            deferred.reject(response);
        });
        return deferred.promise;
    };

    //案件进程--检查项
  caseBarDetailjinchengService.saveTaskCheckedItem = function (params){
    return $http({
      method:"POST",
      url:httpservice.caseTaskCheckUrl,
      data:params,
      headers : {'Content-Type':'application/json'}
    })
  };
  //案件进程--更新
  caseBarDetailjinchengService.updateTaskCheckedItem = function (params){
    return $http({
      method:"POST",
      url:httpservice.caseTaskUpdateUrl,
      data:params,
      headers : {'Content-Type':'application/json'}
    })
  };
  //案件进程--附件预览
  caseBarDetailjinchengService.taskPreviewAttach = function (params) {
    return $http({
      method:"POST",
      url:httpservice.taskPreviewAttachUrl,
      params:params,
      responseType: 'arraybuffer',
      headers : {'Content-Type':'application/json'}
    })
  };

  //案件进程--新增阶段
  caseBarDetailjinchengService.createCaseStage = function (params) {
    return $http({
      method:"GET",
      url:httpservice.createCaseStageUrl,
      params:params
    })
  };

  //案件进程--编辑阶段
  caseBarDetailjinchengService.updateCaseStage = function (params) {
    return $http({
      method:"GET",
      url:httpservice.updateCaseStageUrl,
      params:params
    })
  };

  //案件进程--删除阶段
  caseBarDetailjinchengService.deleteCaseStage = function (params) {
    return $http({
      method:"GET",
      url:httpservice.deleteCaseStageUrl,
      params:params
    })
  };

    return caseBarDetailjinchengService;
};
